package com.dbkj.login;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Ret;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

/**
 * @Author: Max
 * @Date: 2019/10/30 15:08
 */

public class LoginController extends Controller {

    @Inject
    LoginService service;

    @Before(POST.class)
    public void index(){
        //JSONObject obj = JsonKit.parse(getRawData(),JSONObject.class);
        JSONObject obj = JSON.parseObject(getRawData());
        String username = obj.getString("username");
        String password = obj.getString("password");

        if(service.check(username,password)){
            HttpSession session = getSession();
            session.setAttribute("username",username);
            session.setMaxInactiveInterval(18000);
            renderJson(Ret.ok());
        } else {
            renderError(403);
        }
    }

}
